import { illegalArgumentsException } from "@/exception/illegalArgumentsException";
import { categoryApi, subjectApi } from "@/request";
import type { BgwCategory } from "@/typedefined/Category";
import type { SubjectPage } from "@/typedefined/Page";
import type { bgwSubject } from "@/typedefined/Subject";
import { defineStore } from "pinia";
import { ref } from "vue";

export const useTopicStores = defineStore("topic",()=>{
    const currentTopic = ref<BgwCategory>(); // 当前的题目信息
    const subCategories = ref<Array<BgwCategory>>([]); // 二级分类列表
    const currentSubjectList = ref<SubjectPage<bgwSubject>>({
        size:-1,
        current:-1,
        records:[],
        total:-1
    }); // 当前题目列表


    /**
     * @description 根据分类id获取二级分类列表
     * @param categoryId 分类id
     */
    async function getCategoryById(categoryId:number){
        if(!categoryId || categoryId <= 0) throw new illegalArgumentsException("categoryId参数错误");
        const dataList = await categoryApi.getbgwCategoryById(categoryId) as unknown as BgwCategory;
        currentTopic.value = dataList;
        subCategories.value = dataList.towLevelCategoryList;
    }

    /**
     * 获取当前题目列表
     */
    async function getCurrentSubject(params:any,currentPage:number = 1,pageSize:number = 10) {
        const dataPageList = await subjectApi.bgw.searchSubject(params,currentPage,pageSize) as unknown as SubjectPage<bgwSubject>;
        currentSubjectList.value = dataPageList;
    }

    return {
        currentTopic,subCategories,getCategoryById,getCurrentSubject,currentSubjectList
    }
});